//DFS.cpp
//有向图
#include <iostream>
#include <list>
using namespace std;

class Graph
{
    int count = 0;//顶点数
    list<int> *adj;	//邻接表
    void DFSUtil(int v, bool visited[]);
public:
    void addEdge(int v, int w);	//向图中添加边
    void DFS(int v);	//深度优先搜索函数接口
    Graph(int count);
};

Graph::Graph(int count)
{
    this->count = count;
    adj = new list<int>[count];
}

void Graph::addEdge(int v, int w)
{
    adj[v].push_back(w);
}

void Graph::DFSUtil(int v, bool visited[])
{
    visited[v] = true;
    cout << v << " ";
    for(int w:adj[v])
    {
        if (!visited[w])
            DFSUtil(w, visited);
    }
}

void Graph::DFS(int v)
{
    bool *visited = new bool[count];
    for (int i = 0; i < count; ++i)
        visited[i] = false;
    DFSUtil(v, visited);
}

/* 测试 */
int main()
{
    Graph g(3);
    g.addEdge(0, 1);
    g.addEdge(1, 0);
    g.addEdge(2, 2);
    g.addEdge(0, 0);
    g.addEdge(1, 1);
    cout << "从2开始深度搜索" << endl;
    g.DFS(0);
    cout << endl;

    return 0;
}